<template>
  <div class="personnel-detail-container">
    <div class="detail-card">
      <!-- 头部标题 -->
      <div class="header">
        <h2>人员出入记录详情</h2>
      </div>

      <div class="content-wrapper">
        <!-- 左侧信息区域 -->
        <div class="info-section">
          <!-- 基本信息 -->
          <div class="info-group">
            <h3 class="info-title">基本信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">姓名：</span>
                <span class="value name-value">{{ personnelInfo.name }}</span>
              </div>
              <div class="info-item">
                <span class="label">手机号：</span>
                <span class="value phone-value">{{ personnelInfo.phone }}</span>
              </div>
            </div>
          </div>

          <!-- 出入信息 -->
          <div class="info-group">
            <h3 class="info-title">出入信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">出入小区：</span>
                <span class="value">{{ personnelInfo.community }}</span>
              </div>
              <div class="info-item">
                <span class="label">进入时间：</span>
                <span class="value time-value">{{ personnelInfo.entryTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">离开时间：</span>
                <span class="value time-value">{{ personnelInfo.exitTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">出入闸机：</span>
                <span class="value">{{ personnelInfo.gateDevice }}</span>
              </div>
              <div class="info-item">
                <span class="label">出入方式：</span>
                <el-tag :type="getAccessMethodType(personnelInfo.accessMethod)" class="access-method-tag">
                  {{ personnelInfo.accessMethod }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧图片区域 -->
        <div class="image-section">
          <div class="image-container">
            <!-- 显示人脸图片 -->
            <div v-if="personnelInfo.faceImage" class="image-preview">
              <el-image
                  :src="personnelInfo.faceImage.url"
                  :alt="personnelInfo.faceImage.name"
                  fit="cover"
                  class="face-image"
                  :preview-src-list="[personnelInfo.faceImage.url]"
                  hide-on-click-modal
              >
                <template #error>
                  <div class="image-error">
                    <el-icon><User /></el-icon>
                    <span>图片加载失败</span>
                  </div>
                </template>
              </el-image>
              <div class="image-info">
                <div class="image-name">{{ personnelInfo.faceImage.name }}</div>
                <div class="image-time">识别时间：{{ personnelInfo.recognitionTime }}</div>
              </div>
            </div>

            <!-- 无图片时的空状态 -->
            <div v-else class="empty-state">
              <el-icon size="48" color="#c0c4cc"><User /></el-icon>
              <p>暂无照片</p>
              <span class="empty-tip">未采集到人脸识别照片</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    </div>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { User } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// 人员信息数据
const personnelInfo = ref({
  name: '',
  phone: '',
  community: '',
  entryTime: '',
  exitTime: '',
  gateDevice: '',
  accessMethod: '',
  currentStatus: '',
  stayDuration: '',
  faceImage: null,
  recognitionTime: ''
})

// 根据出入方式获取标签类型
const getAccessMethodType = (method) => {
  const typeMap = {
    '人脸识别': 'success',
    '刷卡': 'primary',
    '密码': 'warning',
    '二维码': 'info',
    '手动开门': ''
  }
  return typeMap[method] || ''
}

// 格式化时间显示
const formatTime = (timeString) => {
  if (!timeString) return ''
  try {
    const date = new Date(timeString)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  } catch {
    return timeString
  }
}

// 初始化数据
const initPersonnelInfo = (query) => {
  // 从路由参数获取数据
  console.log('接收到的路由参数:', query)
  // 处理图片URL
  let imageData = null
  if (query.imageUrl && query.imageUrl !== 'null' && query.imageUrl !== 'undefined') {
    imageData = {
      url: query.imageUrl,
      name: '出入照片'
    }
  }

  if (Object.keys(query).length > 0) {
    personnelInfo.value = {
      name: query.name || '--',
      phone: query.phone || '--',
      community: query.community || '--',
      gateDevice: query.gateDevice || '--',
      entryTime: formatTime(query.entryTime) || '--',
      exitTime: formatTime(query.exitTime) || '--',
      accessMethod: query.accessMethod || '--',
      faceImage: query.photo ? {
        url: query.photo,
        name: '人脸识别照片'
      } : null,
      recognitionTime: formatTime(query.entryTime)
    }
  }
}

// 监听路由查询参数变化
watch(
    () => route.query,
    (newQuery) => {
      if (newQuery && Object.keys(newQuery).length > 0) {
        console.log('更新详情数据:', newQuery)
        initPersonnelInfo(newQuery)
      }
    },
    { immediate: true, deep: true }
)
// 计算停留时长
const calculateStayDuration = (entryTime, exitTime) => {
  if (!entryTime) return '--'

  const entry = new Date(entryTime)
  const exit = exitTime ? new Date(exitTime) : new Date()

  const duration = exit - entry
  const hours = Math.floor(duration / (1000 * 60 * 60))
  const minutes = Math.floor((duration % (1000 * 60 * 60)) / (1000 * 60))

  if (hours > 0) {
    return `${hours}小时${minutes}分钟`
  } else {
    return `${minutes}分钟`
  }
}
</script>

<style scoped>
.personnel-detail-container {
  padding: 20px;
  background-color: #ffffff;
  min-height: 100vh;
}

.detail-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  border: 1px solid #e4e7ed;
  max-width: 1000px;
  margin: 0 auto;
}

.header {
  padding: 20px 30px;
  border-bottom: 1px solid #e4e7ed;
  background: white;
}

.header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.content-wrapper {
  display: flex;
  min-height: 500px;
}

/* 左侧信息区域样式 */
.info-section {
  flex: 3;
  padding: 24px;
  border-right: 1px solid #e4e7ed;
}

.info-group {
  margin-bottom: 28px;
}

.info-group:last-child {
  margin-bottom: 0;
}

.info-title {
  font-size: 15px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  padding-left: 8px;
  border-left: 3px solid #409eff;
}

.info-grid {
  display: grid;
  gap: 14px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  background: #f8f9fa;
  border-radius: 6px;
  min-height: 48px;
  transition: background-color 0.2s;
}

.info-item:hover {
  background: #f0f2f5;
}

.info-item .label {
  font-weight: 500;
  color: #606266;
  min-width: 90px;
  font-size: 14px;
}

.info-item .value {
  color: #303133;
  font-weight: 500;
  font-size: 14px;
}

.name-value {
  color: #409eff;
  font-size: 16px;
  font-weight: 600;
}

.phone-value {
  color: #67c23a;
  font-weight: 500;
}

.time-value {
  color: #e6a23c;
}

.duration-value {
  color: #f56c6c;
  font-weight: 600;
}

.access-method-tag,
.status-tag {
  font-weight: 500;
  font-size: 12px;
  padding: 6px 10px;
}

/* 右侧图片区域样式 */
.image-section {
  flex: 2;
  min-width: 280px;
  max-width: 350px;
  padding: 24px;
  background: white;
  display: flex;
  flex-direction: column;
}

.image-title {
  font-size: 15px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 20px;
  text-align: center;
}

.image-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 200px;
}

.image-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
  gap: 16px;
}

.face-image {
  width: 100%;
  height: 220px;
  border-radius: 8px;
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  object-fit: cover;
}

.image-info {
  text-align: center;
}

.image-name {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
  margin-bottom: 6px;
}

.image-time {
  font-size: 12px;
  color: #909399;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #909399;
  gap: 8px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 220px;
  color: #c0c4cc;
  background: #fafafa;
  border-radius: 8px;
  border: 1px dashed #dcdfe6;
  gap: 12px;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
  color: #909399;
}

.empty-tip {
  font-size: 12px;
  color: #c0c4cc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-wrapper {
    flex-direction: column;
  }

  .info-section {
    border-right: none;
    border-bottom: 1px solid #e4e7ed;
  }

  .image-section {
    min-width: 100%;
    max-width: 100%;
    height: 280px;
  }

  .face-image {
    height: 200px;
  }

  .empty-state {
    height: 200px;
  }
}

@media (max-width: 480px) {
  .personnel-detail-container {
    padding: 12px;
  }

  .info-section {
    padding: 16px;
  }

  .image-section {
    padding: 16px;
  }

  .info-item {
    padding: 10px 12px;
  }

  .info-item .label {
    min-width: 80px;
    font-size: 13px;
  }

  .info-item .value {
    font-size: 13px;
  }
}
</style>